<template>
	<view class="integral-mall">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view class="tehui-top-img">
				<image src="../../static/images/tehui-img.png" mode=""></image>
			</view>
			<view  class="gouwuquan-title">
				<image src="../../static/images/tehui.png"></image>
			</view>
			<view class="main">
				<view class="goods-lists" :class="{
					'goods-lists--one': goodsType === 'one'
				}">
					<view class="goods-item" v-for="(item, index) in goodsLists" :key="index">
						<router-link :to="`/pages/goods_details/goods_details?id=${item.id}&page=gouwuquan`">
							<view class="goods-item-info">
								<view class="goods-image">
									<view class="image-wrap">
										<u-image :src="item.image" width="100%" height="100%"></u-image>
									</view>
								</view>
								<view class="goods-info">
									<view class="goods-name line-2">
										{{ item.name }}
									</view>
									<view class="yishou-yuanjia">
										<image src="../../static/images/guanfang-price.png"></image>
										<view>已售200件</view>
									</view>
									<view class="goods-price">
										<view class="xianjia">
											￥<text>1238</text>
										</view>
										<view class="goumai-btn">
											立即兑换
										</view>
									</view>
								</view>
							</view>
						</router-link>
					</view>
				</view>
			</view>

		</mescroll-body>
	</view>
</template>

<script>
	import {
		getIntegralGoods
	} from '@/api/activity'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins";
	import {trottle} from '@/utils/tools'
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				sortConfig: [{
						name: '最新',
						type: 'sort_by_new',
						value: 'desc',
						setValue: 'desc'
					},
					{
						name: '积分',
						type: 'sort_by_integral',
						value: ''
					},
					{
						name: '兑换量',
						type: 'sort_by_sales',
						value: '',
						setValue: 'desc'
					}
				],
				goodsType: 'double',
				upOption: {
					noMoreSize: 2,
					auto: false,
					empty: {
						icon: '/static/images/goods_null.png',
						tip: "暂无商品",
					}
				},
				goodsLists: [],
				integral: ''
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			changeGoodsType() {
				this.goodsType = this.goodsType === 'one' ? 'double' : 'one'
			},
			handleSort(current) {
				this.sortConfig.forEach((item) => {
					if(current.type != item.type) {
						item.value = ''
						return
					}
					if(item.setValue) {
						item.value = item.setValue
						return 
					}
					if(item.value == 'asc') {
						item.value = 'desc'
					}else{
						item.value ='asc'
					}
				})
				
			},
			upCallback(page) {
				const pageNum = page.num; // 页码, 默认从1开始
				const pageSize = page.size; // 页长, 默认每页10条
				const sort = this.sortConfig.find((item) => item.value) || {}
				getIntegralGoods({
					[sort.type]: sort.value,
					page_no: pageNum,
					page_size: pageSize
				}).then(res => {
					if (res.code == 1) {
						const {
							goods,
							integral
						} = res.data
						this.integral = integral
						let curPageData = goods.lists;
						let curPageLen = curPageData.length;
						let hasNext = !!goods.more;
						if (pageNum == 1) this.goodsLists = [];
						this.goodsLists = this.goodsLists.concat(curPageData);
						this.mescroll.endSuccess(curPageLen, hasNext);
					}else{
						this.mescroll.endErr();
					}
				}).catch(() => {
					this.mescroll.endErr();
				})
			},
		},
		watch: {
			sortConfig: {
				handler(val) {
					this.goodsLists = []
					this.mescroll.resetUpScroll()
				},
				deep: true
			}
		},
		onLoad() {
			this.handleSort = trottle(this.handleSort, 500, this);
		}

	}
</script>

<style lang="scss">
	page {
		padding: 0;
	}
	$goods-margin: 8rpx;

	.integral-mall {
		.header {
			padding: 22rpx 24rpx;
		}

		.main {
			.sort-bar {
				height: 80rpx;

				.sort-bar-item {
					height: 100%;
				}

				.arrow-icon {
					transform: scale(0.4);
				}
			}

			.goods-lists {
				padding: 20rpx 20rpx 0;
				margin: -$goods-margin;
				display: flex;
				flex-wrap: wrap;
				.goods-item {

					width: 50%;

					.goods-item-info {
						margin: $goods-margin;
						border-radius: 14rpx;
						background: #fff;
						overflow: hidden;

						.goods-image {
							flex: none;
							position: relative;
							height: 0;
							padding-top: 100%;

							.image-wrap {
								position: absolute;
								top: 0;
								left: 0;
								right: 0;
								bottom: 0;
							}
						}

						.goods-info {
							padding: 14rpx;

							.goods-name {
								margin-top: 46rpx;
							}
						}
					}
				}

				&--one {
					.goods-item {
						width: 100%;

						.goods-item-info {
							display: flex;

							.goods-image {
								width: 200rpx;
								padding-top: 200rpx;
							}

							.goods-info {
								padding: 24rpx 20rpx;
							}
						}
					}
				}
			}
		}
	}
	.bg-top {
		width: 100%;
		height: 633rpx;
		background-image: url('../../static/images/jifen-top.png');
		background-size: 100%;
		background-repeat: no-repeat;
	}
	.my-jifen {
		display: flex;
		justify-content: space-between;
		margin-left: 60rpx;
		margin-top: 72rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #FFFFFF;
	}
	.jifen-num {
		margin-top: 10rpx;
		font-size: 90rpx;
	}
	.title-top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		image {
			width: 16rpx;
			height: 29rpx;
		}
		.title {
			font-weight: bold;
			font-size: 36rpx;
			color: #FFFFFF;
		}
		.rule {
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}
	.jifen-order {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 177rpx;
		height: 49rpx;
		background: #0271E4;
		border-radius: 25rpx;
		font-weight: 400;
		font-size: 28rpx;
		image{
			width: 11rpx;
			height: 20rpx;
			margin-left: 6rpx;
			transform: rotate(180deg);
		}
	}
	.vip-img {
		width: 206rpx;
		height: 248rpx;
		margin-right: 60rpx;
	}
	.content-top {
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;
		view {
			display: flex;
			align-items: center;
		}
		.line {
			width: 6rpx;
			height: 32rpx;
			margin-right: 20rpx;
			background: #0271E4;
			border-radius: 3rpx;
		}
		.cai-box {
			font-weight: bold;
			font-size: 36rpx;
			color: #333333;
		}
		.see-more {
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			image {
				width: 40rpx;
				height: 40rpx;
				transform: rotate(270deg);
			}
		}
	}
	.ke-de-jifen {
		margin-top: 16rpx;
		font-weight: 400;
		font-size: 24rpx;
		color: #0271E4;
	}
	.yishou-yuanjia {
		display: flex;
		justify-content: space-between;
		margin-top: 11rpx;
		font-weight: 400;
		font-size: 20rpx;
		color: #999999;
		.yuanjia {
			text-decoration: line-through;
		}
		image {
			width: 105rpx;
			height: 28rpx;
		}
	}
	.goods-price {
		display: flex;
		justify-content: space-between;
		margin-top: 14rpx;
		.xianjia {
			color: #E63A26;
			font-size: 24rpx;
			font-weight: bold;
			text {
				font-size: 36rpx;
			}
		}
		.goumai-btn {
			width: 117rpx;
			height: 41rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #0271E4;
			border-radius: 21rpx;
			font-weight: 400;
			font-size: 20rpx;
			color: #FFFFFF;
		}
	}
	.gouwuquan-top {
		width: 710rpx;
		height: 220rpx;
		margin: 20rpx auto 0;
		background-image: url('../../static/images/gouwuquan-bg.png');
		background-size: 100%;
		background-repeat: no-repeat;
		.title {
			padding-top: 42rpx;
			margin-left: 36rpx;
			font-weight: 400;
			font-size: 26rpx;
			color: #FFFFFF;
		}
		.num {
			margin-top: 10rpx;
			margin-left: 36rpx;
			font-weight: 400;
			font-size: 90rpx;
			color: #FFFFFF;
		}
	}
	.gouwuquan-title {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 50rpx;
		image {
			width: 269rpx;
			height: 42rpx;
		}
	}
	.tehui-top-img {
		margin-top: 30rpx;
		text-align: center;
		image {
			width: 709rpx;
			height: 316rpx;
		}
	}
</style>
